package com.itheima.cs;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.itheima.cs.dao.UserDao;
import com.itheima.cs.entity.Userinfo;

public class ChangePasswordActivity extends AppCompatActivity {

    private EditText etUsername, etPhone, etOldPassword, etNewPassword, etRepeatNewPassword;
    private Button btnChangePassword;
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        etUsername = findViewById(R.id.et_username);
        etPhone = findViewById(R.id.et_phone);
        etOldPassword = findViewById(R.id.et_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        etRepeatNewPassword = findViewById(R.id.et_repeat_new_password);
        btnChangePassword = findViewById(R.id.btn_change_password);
        userDao = new UserDao();
        btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });
    }

    private void changePassword() {
        String username = etUsername.getText().toString();
        String phone = etPhone.getText().toString();
        String oldPassword = etOldPassword.getText().toString();
        String newPassword = etNewPassword.getText().toString();
        String repeatNewPassword = etRepeatNewPassword.getText().toString();

        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            etUsername.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            etPhone.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(oldPassword)) {
            Toast.makeText(this, "请输入原密码", Toast.LENGTH_SHORT).show();
            etOldPassword.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(newPassword)) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
            etNewPassword.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(repeatNewPassword)) {
            Toast.makeText(this, "请重复新密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!newPassword.equals(repeatNewPassword)) {
            Toast.makeText(this, "新密码与重复新密码不匹配", Toast.LENGTH_SHORT).show();
            return;
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                Userinfo user = userDao.getUserByUsername(username);
                String resultMessage;

                if (user == null) {
                    resultMessage = "用户不存在";
                } else if (!user.getPhone().equals(phone) || !user.getPassword().equals(oldPassword)) {
                    resultMessage = "手机号或原密码错误";
                } else {

                    boolean isUpdated = userDao.updateUserPassword(username, newPassword);
                    resultMessage = isUpdated ? "密码修改成功" : "密码修改失败";
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ChangePasswordActivity.this, resultMessage, Toast.LENGTH_SHORT).show();
                        if (resultMessage.equals("密码修改成功")) {
                            finish();
                        }
                    }
                });
            }
        }).start();
    }
}